package com.dominator.utils.wechat.utils;

import com.dominator.utils.system.PropertiesLoader;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;

/**
 * 读取证书
 * @author 王士忠
 *
 */
@SuppressWarnings("deprecation")
public class ReadSSl {

	private static String  charset="UTF-8";


	public static PropertiesLoader propertiesLoader = new PropertiesLoader("sysconfig.properties");




	private static ReadSSl readSSL = null;
	
	private ReadSSl(){
		
	}
	
	public static ReadSSl getInstance(){
		if(readSSL == null){
			readSSL = new ReadSSl();
		}
		return readSSL;
	}


	/**
	 *  读取 apiclient_cert.p12 证书
	 * @return
	 * @throws Exception
	 */
	public  SSLConnectionSocketFactory readCustomSSL() throws Exception{
	    KeyStore keyStore  = KeyStore.getInstance("PKCS12");
        FileInputStream instream = new FileInputStream(new File(propertiesLoader.getProperty("wx_ca_license")));
        try {
            keyStore.load(instream, propertiesLoader.getProperty("wx_mch_public_id").toCharArray());
        } finally {
            instream.close();
        }
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, propertiesLoader.getProperty("wx_mch_public_id").toCharArray()).build();
        
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] { "TLSv1" }, null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        return sslsf;
	}

}
